from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.response import Response
from home import models,homeser
from django.core.cache import cache

class Header(APIView):
    def get(self, request):
        #获取头部导航有效数据 并且排序
        navdata=models.Nav.objects.filter(position=1,isdel=False).order_by('sort')
        ser=homeser.NavSerializer(navdata,many=True)#列表数据序列化
        return Response({"msg":"ok","code":200,"data":ser.data})
class Footer(APIView):
    def get(self, request):
        #获取头部导航有效数据 并且排序
        navdata=models.Nav.objects.filter(position=0,isdel=False).order_by('sort')
        ser=homeser.NavSerializer(navdata,many=True)#列表数据序列化
        return Response({"msg":"ok","code":200,"data":ser.data})

class Banner(APIView):
    def get(self, request):
        #缓存中有数据 先读缓存  没有数据查数据库 写如缓存 再返回  key=banner
        bannerdata=cache.get('banner')
        if bannerdata:
            return Response({"msg":"ok","code":200,"data":bannerdata})
        #获取头部导航有效数据 并且排序
        navdata=models.Banner.objects.filter(isdel=False).order_by('sort')
        ser=homeser.BannerSerializer(navdata,many=True)#列表数据序列化
        #写入缓存
        cache.set('banner',ser.data,60*60)
        return Response({"msg":"ok","code":200,"data":ser.data})




